package com.chebaohui.iqs.service.impl.deal;

import java.util.Date;

import com.chebaohui.iqs.config.CompanyValue;
import com.chebaohui.iqs.config.ResultCode;
import com.chebaohui.iqs.entity.base.Company;
import com.chebaohui.iqs.entity.req.CarPrice;
import com.chebaohui.iqs.entity.res.CompanyCarPrice;

/**
 * 车险算费任务线程
 * 
 * @author xielonghai
 *
 */
public class PriceThread extends Thread {

	protected Company company;// 所需返回保险公司编码

	protected CarPrice param;// 报价参数

	protected CompanyCarPrice result;// 返回结果

	protected long begin;// 线程开始时间

	protected long dealTime = 0l;

	public PriceThread(CarPrice param, Company company) {
		super();
		this.param = param;
		this.company = company;
	}

	/**
	 * 获取算费结果
	 * 
	 * @return
	 */
	public CompanyCarPrice getResult() {
		int time = 0;
		while (true) {
			if (!this.isAlive() || time >= CompanyValue.maxTime)
				break;

			time++;
			try {
				Thread.sleep(1000);// 每隔1秒查看线程是否完成
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			if (this.isAlive()) {// 关闭线程
				this.interrupt();
				this.join();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (null == result) {
			result = new CompanyCarPrice();
			result.setCompanyCode(company.getCompanyCode());
			result.setModelCode(company.getModelCode());
			result.setCode(ResultCode.N02.getCode());
			result.setMsg(ResultCode.N02.getMsg() + "线程处理超时");
			dealTime = new Date().getTime() - begin;
		}
		return result;
	}

	/**
	 * 线程处理时间
	 * 
	 * @return
	 */
	public long getDealTime() {
		return dealTime;
	}
}
